package com.mstar.qs.basic.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.morningstar.qs.api.basic.common.config.GlobalConfig;

public class ServletContextInitListener implements ServletContextListener {

	public static final String IDENTIFIER_OF_GLOBAL_CONFIG = "global-config";
	public static final String IDENTIFIER_OF_LOG4J = "log4j";
	// public static final String IDENTIFIER_OF_HIBERNATE_CONFIG =
	// "hibernate_config";
	public static final String CONTEXT = "Context";

	private Logger newLog;

	private static String log4j_filename = "log4j.properties";

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		java.util.logging.Logger log = java.util.logging.Logger
				.getLogger(ServletContextInitListener.class.getName());
		log.info(GlobalConfig.LINE_SEPARATOR);
		log.info(GlobalConfig.LINE_SEPARATOR);
		log.warning("\t\t -------- contextInitialized: "
				+ arg0.getServletContext().getContextPath());
		ServletContext sc = arg0.getServletContext();

		try {
			initLog4j(sc);
			log.info(GlobalConfig.LINE_SEPARATOR);
			initGlobalConfig(sc);
			log.info(GlobalConfig.LINE_SEPARATOR);
			log.info(GlobalConfig.LINE_SEPARATOR);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initLog4j(ServletContext sc) {
		java.util.logging.Logger log = java.util.logging.Logger
				.getLogger(ServletContextInitListener.class.getName());
		String context = sc.getContextPath();
		log.warning(" -------- Start to initialize log4j within context: "
				+ sc.getContextPath());
		String fileName = sc.getInitParameter(IDENTIFIER_OF_LOG4J);
		if (fileName != null) {
			log.warning("\t\t -------- Found out configured log property file "
					+ fileName + " with the key '" + IDENTIFIER_OF_LOG4J + "'");
			log4j_filename = fileName;
			PropertyConfigurator.configureAndWatch(fileName, 1000 * 60);
			log.warning("\t\t -------- Log4j initialized successfully");
		} else {
			log.warning("\t\t -------- Does not find out log property file with key '"
					+ IDENTIFIER_OF_LOG4J + "'");
			String especialPath = null;
			if (System.getProperty("os.name").toString().indexOf("Windows") != -1) {
				especialPath = GlobalConfig.GLOBAL_CONFIG_FILE_PATH_WINDOW;
			}
			if (especialPath != null) {
				log.warning("\t\t -------- Init log4j with the especial file name: "
						+ especialPath + "/" + log4j_filename);
				PropertyConfigurator.configureAndWatch(especialPath + "/"
						+ log4j_filename, 1000 * 60);
			}
		}
		newLog = org.apache.log4j.Logger
				.getLogger(ServletContextInitListener.class.getPackage()
						.getName());

	}

	private void initGlobalConfig(ServletContext sc) throws Exception {
		String context = sc.getContextPath();
		newLog.warn("\t\t -------- Start to init Global Config within context: "
				+ context);
		String fileName = sc.getInitParameter(IDENTIFIER_OF_GLOBAL_CONFIG);
		if (fileName != null) {
			newLog.warn("\t\t -------- Global config file name " + fileName);
			try {
				GlobalConfig.configure(fileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			String temp = null;
			if (System.getProperty("os.name").toString().indexOf("Windows") != -1) {
				temp = GlobalConfig.GLOBAL_CONFIG_FILE_PATH_WINDOW;
				try {
					GlobalConfig.configure(temp + "/dev.xml");
					newLog.info("Initialized the global config from the specifically directory for test purpose.");
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else

				newLog.warn("\t\t -------- !!! Cannot found out Global Config file in the initial parameters with key '"
						+ IDENTIFIER_OF_GLOBAL_CONFIG + "'");
		}

	}

}
